// pages/setMoney/setMoney.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    canClick: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    console.log()
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        })
      }
    })
  },
  inputMoney: function (e) {
    if (e.detail.value != '') {
      if (parseFloat(e.detail.value) < parseFloat(10)){
        var aa = (parseFloat(e.detail.value) + parseFloat(0.1)).toFixed(2)
      }else{
        var aa = (parseFloat(e.detail.value) + parseFloat(e.detail.value * 0.01)).toFixed(2)
      }
      this.setData({
        getInputMoney: e.detail.value,
        allMoney: aa
      })
    } else {
      this.setData({
        allMoney: 0
      })
    }
  },
  inputNum: function (e) {
    if (e.detail.value != '') {
      this.setData({
        getInputNum: e.detail.value
      })
    }
  },
  inputAnswer: function (e) {
    if (e.detail.value != '') {
      this.setData({
        getInputAnswer: e.detail.value
      })
    }
  },
  setMoneys: function () {
    var that = this;
    if (this.data.canClick) {
      that.setData({
        canClick: false
      })
      setTimeout(function () {
        that.setData({
          canClick: true
        })
      }, 6000)
    } else {
      wx.showToast({
        title: '处理中，请勿重复提交',
      })
      return;
    }

    if (this.data.allMoney == "NaN") {
      wx.showModal({
        title: '提示',
        content: '红包金额必须为数字或者小数',
      })
      return;
    }
    var reg = /(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/;
    var numReg = /^\+?[1-9][0-9]*$/;
    if (!reg.test(this.data.allMoney)) {
      wx.showModal({
        title: '提示',
        content: '红包金额必须为数字或者小数',
      })
      return;
    }
    if (!numReg.test(this.data.getInputNum) || !numReg.test(this.data.getInputAnswer)) {
      console.log(typeof (this.data.getInputNum))
      wx.showModal({
        title: '提示',
        content: '红包个数跟答题个数必须为正整数',
      })
      return;
    }
    if (parseInt(this.data.getInputNum) > parseInt(this.data.allMoney)) {
      console.log(typeof (this.data.getInputNum))
      wx.showModal({
        title: '提示',
        content: '每人所得最小红包金额不得小于1元',
      })
      return;
    } if (parseFloat(this.data.getInputMoney) > parseFloat(3000) || parseInt(this.data.getInputMoney) < parseFloat(1)) {
      console.log(typeof (this.data.getInputNum))
      wx.showModal({
        title: '提示',
        content: '红包金额最高不超过3000',
      })
      return;
    }
    var problems = [];
    problems = wx.getStorageSync('userSetQuestion');
    console.log(problems)
    var _json = {
      user_id: wx.getStorageSync("userid"),
      problem: problems,
    }
    if (!wx.getStorageSync("userid")) {
      wx.showModal({
        title: '提示',
        content: '查询到您当前未授权用户信息，请删除小程序后重新获取',
      })
    }
    wx.request({
      url: app.globalData.allUrl + "/api/save_question.php",
      data: {
        user_id: wx.getStorageSync("userid"),
        problem: problems,
        package_num: that.data.getInputNum,
        answer_num: that.data.getInputAnswer,
        money: that.data.getInputMoney,
        total_money: that.data.allMoney,
      },
      header: { 'content-type': "application/json" },
      method: 'POST',
      success: function (res) {
        console.log(res.data)
        wx.setStorageSync("userCurrentPutQuestionId", res.data.question_id)
        wx.request({
          url: app.globalData.allUrl + "/api/wxpay.php",
          data: {
            user_id: wx.getStorageSync("userid"),
            order_id: res.data.order_id,
            money: that.data.allMoney,
          },
          header: { 'content-type': "application/x-www-form-urlencoded" },
          method: 'POST',
          success: function (res) {
            console.log(res.data)
            var pays = res.data;
            wx.requestPayment({
              timeStamp: pays.timeStamp,
              nonceStr: pays.nonceStr,
              package: pays.package,
              signType: pays.signType,
              paySign: pays.paySign,
              success: function (res) {
                console.log(res)
                wx.redirectTo({
                  url: '../../pages/myquestions/myquestions?question_id=' + wx.getStorageSync('userCurrentPutQuestionId'),
                })
              }, fail(res) {
                wx.showModal({
                  title: '提示',
                  content: '支付失败！',
                })
              }
            })
          }, fail: function () {
            wx.showModal({
              title: '提示',
              content: '保存问题失败！请稍后重试...',
            })
          }
        })
      }, fail: function () {
        wx.showModal({
          title: '提示',
          content: '保存问题失败！请稍后重试...',
        })
      }
    })
    console.log(this.data.allMoney + "," + this.data.getInputMoney + "," + this.data.getInputNum + "," + this.data.getInputAnswer + ",")
  }
})